//
//  YBReceiveViewController.m
//  Nectar
//
//  Created by famer on 2023/11/8.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBReceiveViewController+LayoutConstraints.h"
#import <CoreImage/CoreImage.h>
#import "YBNavigationBar.h"
#import "HomeCoinManager.h"

@interface YBReceiveViewController ()

@end

@implementation YBReceiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kThemeColor;
    self.supportLabel.text = [NSString stringWithFormat:@"only supports %@ assets %@", [HomeCoinManager shareInstance].globalSymbol, [HomeCoinManager shareInstance].globalName];
    
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.contentView];
    [self.contentView addSubview:self.supportLabel];
    [self.contentView addSubview:self.qrCodeImageView];
    [self.contentView addSubview:self.walletAddressLabel];
    [self.contentView addSubview:self.walletAddressValueLabel];
    
    [self.contentView addSubview:self.bottomContentView];
    [self.bottomContentView addSubview:self.shareContentView];
    [self.shareContentView addSubview:self.shareImageView];
    [self.shareContentView addSubview:self.shareLabel];
    
    [self.bottomContentView addSubview:self.copyContentView];
    [self.copyContentView addSubview:self.copyImageView];
    [self.copyContentView addSubview:self.copyLabel];
    
    [self setLayoutConstraints];
    
    [self updateUI];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIImage *image = [self generateQRCodeWithString:self.walletAddress];
    self.qrCodeImageView.image = image;
    
    self.walletAddressValueLabel.text = self.walletAddress;
}

#pragma mark - Private Method
- (void)updateUI{
    self.contentView.layer.cornerRadius = 10;
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bottomContentView.bounds
                                                       byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
                                                             cornerRadii:CGSizeMake(10.0, 10.0)];
    
    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = self.bottomContentView.bounds;
    maskLayer.path = maskPath.CGPath;
    
    self.bottomContentView.layer.mask = maskLayer;

}

- (UIImage *)generateQRCodeWithString:(NSString *)string {
    // 创建一个字符串的数据
    NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
    
    // 创建一个 CIFilter 来生成二维码
    CIFilter *qrCodeFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrCodeFilter setDefaults];
    [qrCodeFilter setValue:stringData forKey:@"inputMessage"];
    
    // 生成二维码图像
    CIImage *qrCodeImage = qrCodeFilter.outputImage;
    qrCodeImage = [qrCodeImage imageByApplyingTransform:CGAffineTransformMakeScale(8, 8)];

    // 创建一个用于显示图像的上下文
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:qrCodeImage fromRect:qrCodeImage.extent];
    
    // 将 Core Image 生成的图像转换为 UIImage
    UIImage *qrCodeUIImage = [UIImage imageWithCGImage:cgImage];
    
    // 释放 CGImage
    CGImageRelease(cgImage);
    
    return qrCodeUIImage;
}

#pragma mark - Getter Setter
YBViewGetter(contentView, _contentView, [UIColor hexColor:@"FFFFFF"])
YBLabelGetter(supportLabel, _supportLabel, [UIFont systemFontOfSize:12], @"only supports", kThemeBlackColor, NSTextAlignmentCenter);
YBLabelGetter(walletAddressLabel, _walletAddressLabel, [UIFont systemFontOfSize:12], @"Wallet Address", kThemeBlackColor, NSTextAlignmentCenter);
YBLabelGetter(walletAddressValueLabel, _walletAddressValueLabel, [UIFont systemFontOfSize:12], @"*", kThemeBlackColor, NSTextAlignmentCenter);

YBViewGetter(bottomContentView, _bottomContentView, [UIColor hexColor:@"F4F5F7"])
YBViewGetter(shareContentView, _shareContentView, [UIColor hexColor:@"F4F5F7"])
YBViewGetter(copyContentView, _copyContentView, [UIColor hexColor:@"F4F5F7"])

YBImageViewGetter(shareImageView, _shareImageView, [UIImage imageNamed:@"icon_share"])
YBImageViewGetter(copyImageView, _copyImageView, [UIImage imageNamed:@"icon_copy"])
YBImageViewGetter(qrCodeImageView, _qrCodeImageView, nil)

YBLabelGetter(shareLabel, _shareLabel, [UIFont systemFontOfSize:12], @"Share", kThemeBlackColor, NSTextAlignmentCenter);
YBLabelGetter(copyLabel, _copyLabel, [UIFont systemFontOfSize:12], @"Copy", kThemeBlackColor, NSTextAlignmentCenter);


- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kClearColor;
        [_navigationBar setTitle:@"Wallet under identity" titleColor:kWhiteColor];
        [_navigationBar setLeftBarImage:@"icon_nav_back_white" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}


@end
